/*
	Webpacus template editor
*/

var rec = null;
var url = null;
var template_filename = null;

var pending_js = null;

var css_rnd = 0;

function _ts(text) {
	var el = $('div_template_status');
	if (el) el.innerHTML = text;
}

function _cs(text) {
	var el = $('div_css_status');
	if (el) el.innerHTML = text;
}

function load_rec(nr, tmp_template_filename) {

	if (! nr) {
		Logger.error('load_rec called without nr');
		return false;
	}

	var rec_uri = $F('rec_uri_prefix');
	if (! rec_uri) Logger.error('rec_uri_prefix field not found in form!');
	rec_uri += '/' + nr;

	if (! tmp_template_filename) tmp_template_filename = template_filename;
	Logger.info('load_rec '+rec_uri+' in '+tmp_template_filename);

	if (loading.record) {
		Logger.info('loading of record '+rec_uri+' skipped, load in progress');
		return false;
	}

	show_working();
	$('record_nr').disabled = 'true';

	if (nr == 1) {
		Element.addClassName('a_left_arr', 'nav_disable');
	} else {
		Element.removeClassName('a_left_arr', 'nav_disable');
	}

	var args = '?record_uri='+rec_uri+'&template_filename='+tmp_template_filename;

	new Ajax.Updater( 'div_record',  url+'record'+args, {
		asynchronous: 1,
		onLoading: function(request) {
			loading.record = 1;
			Logger.info('load_rec.onLoading: '+nr);
		},
		onLoaded: function(request) {
			loading.record = 0;
			hide_working();
			$('record_nr').value = nr;
			$('record_nr').disabled = '';
			rec = nr;
			Logger.info('load_rec.onLoaded: '+nr);
		}
	} ) ;

	return false;
}

function inc_rec() {
	show_working();
	rec++;
	load_rec(rec);
	hide_working();
	return false;
}

function dec_rec() {
	if (rec > 1) {
		show_working();
		rec--;
		load_rec(rec);
		hide_working();
	}
	return false;
}

function reload_rec() {
	show_working();
	load_rec(rec);
	hide_working();
	return false;
}

var current_edit = '';

function edit_template() {
	Element.hide('row_css');

	Element.removeClassName('a_css', 'tab_selected');
	Element.addClassName('a_template', 'tab_selected');

	Element.show('row_template');

	Logger.debug("switched to template editor");
	var c = $('template_content');
	if (c) c.focus();
	Logger.debug('zIndex template:'+$('row_template').style.zIndex+' css:'+$('row_css').style.zIndex);
	return false;
}

function edit_css() {

	Element.hide('row_template');

	Element.removeClassName('a_template', 'tab_selected');
	Element.addClassName('a_css', 'tab_selected');

	Element.show('row_css');

	Logger.debug("switched to CSS editor");
	var c = $('css_content');
	if (c) c.focus();
	Logger.debug('zIndex template:'+$('row_template').style.zIndex+' css:'+$('row_css').style.zIndex);
	return false;
}

function switch_template(new_template_filename) {

	Logger.info('switch_template to '+new_template_filename);
	show_working();

	Logger.debug('load template editor');
	template_filename = new_template_filename;
	load_template(new_template_filename);

	Logger.debug('refresh record');
	load_rec(rec);

	edKill('template_content');

	hide_working();

	return false;
}

var loading = {
	template: 0,
	css: 0,
	record: 0
};

function load_template( template_filename ) {

	if (loading.template) {
		Logger.info('loading of template '+template_filename+' skipped, load in progress');
		return;
	}

	show_working();

	var args = '?template_filename='+template_filename;

	new Ajax.Updater( 'row_template',  url+'template'+args, {
		asynchronous: 1,
		onLoading: function(request) {
			loading.template = 1;
			Logger.info('load_template.onLoading: '+template_filename);
		},
		onLoaded: function(request) {
			loading.template = 0;
			hide_working();
			Logger.info('load_template.onLoaded: '+template_filename);
		}
	} ) ;
}

function load_css( css_filename ) {

	if (loading.css) {
		Logger.info('loading of css '+css_filename+' skipped, load in progress');
		return;
	}

	var css_url = url + 'css?css_filename='+css_filename;

	Logger.info('load_css '+css_url);

	show_working();

	new Ajax.Updater( 'row_css', css_url, {
		asynchronous: 1,
		onLoading: function(request) {
			loading.css = 1;
			Logger.info('load_css.onLoading: '+css_filename);
		},
		onLoaded: function(request) {
			loading.css = 0;
			hide_working();
			Logger.info('load_css.onLoaded: '+css_filename);
			reload_css( css_filename );
		}
	} ) ;
};

function reload_css( css_filename ) {

	if (! css_filename) css_filename = 'user.css';

	css_rnd++;
	var css_url = '/css/' + css_filename + '?' + css_rnd;
	Logger.info('reload_css from '+css_url);
	$('user_css_link').href = css_url;

	return false;
}

var working_count = 0;

function show_working() {
	working_count++;
	if (working_count > 1) Element.show('working');
	Logger.debug('show_working, count = '+working_count);
}

function hide_working() {
	working_count--;
	if (working_count < 1) Element.hide('working');
	Logger.debug('hide_working, count = '+working_count);
}

/* called body.onLoad */

function init_page() {
	show_working();

	textarea_reset_size( 'template_content' );
	textarea_reset_size( 'css_content' );

	edit_template();

	hide_working();
}

/* resize textarea */

function textarea_reset_size( id ) {
	var textarea = $( id );
	if (! textarea) {
		Logger.error('can\'t reset size to non-existant textarea '+id);
		return;
	}
	var hgt = Cookie.get('webpacus_ta_'+id);
	if (! hgt) hgt='20em';
	textarea.style.height = hgt;
	Logger.debug('reset textarea '+id+' height to '+hgt);
}

function textarea_resize( id, val ){
	var textarea = $( id );
	var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2));
	height += val;
	Logger.debug('resize_textarea('+id+','+val+') '+textarea.style.height+' '+height);
	textarea.style.height = height+'px';
	Cookie.set('webpacus_ta_'+id,textarea.style.height,365);
}
